package org.jivesoftware.smackx.commands;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.packet.AdHocCommandData;

public class RemoteCommand extends AdHocCommand
{
  private Connection connection;
  private String jid;
  private long packetReplyTimeout;
  private String sessionID;

  protected RemoteCommand(Connection paramConnection, String paramString1, String paramString2)
  {
    this.connection = paramConnection;
    this.jid = paramString2;
    setNode(paramString1);
    this.packetReplyTimeout = SmackConfiguration.getPacketReplyTimeout();
  }

  private void executeAction(AdHocCommand.Action paramAction, long paramLong)
    throws XMPPException
  {
    executeAction(paramAction, null, paramLong);
  }

  private void executeAction(AdHocCommand.Action paramAction, Form paramForm, long paramLong)
    throws XMPPException
  {
    AdHocCommandData localAdHocCommandData1 = new AdHocCommandData();
    localAdHocCommandData1.setType(IQ.Type.SET);
    localAdHocCommandData1.setTo(getOwnerJID());
    localAdHocCommandData1.setNode(getNode());
    localAdHocCommandData1.setSessionID(this.sessionID);
    localAdHocCommandData1.setAction(paramAction);
    if (paramForm != null)
      localAdHocCommandData1.setForm(paramForm.getDataFormToSend());
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localAdHocCommandData1.getPacketID()));
    this.connection.sendPacket(localAdHocCommandData1);
    Packet localPacket = localPacketCollector.nextResult(paramLong);
    localPacketCollector.cancel();
    if (localPacket == null)
      throw new XMPPException("No response from server on status set.");
    if (localPacket.getError() != null)
      throw new XMPPException(localPacket.getError());
    AdHocCommandData localAdHocCommandData2 = (AdHocCommandData)localPacket;
    this.sessionID = localAdHocCommandData2.getSessionID();
    super.setData(localAdHocCommandData2);
  }

  public void cancel()
    throws XMPPException
  {
    executeAction(AdHocCommand.Action.cancel, this.packetReplyTimeout);
  }

  public void complete(Form paramForm)
    throws XMPPException
  {
    executeAction(AdHocCommand.Action.complete, paramForm, this.packetReplyTimeout);
  }

  public void execute()
    throws XMPPException
  {
    executeAction(AdHocCommand.Action.execute, this.packetReplyTimeout);
  }

  public void execute(Form paramForm)
    throws XMPPException
  {
    executeAction(AdHocCommand.Action.execute, paramForm, this.packetReplyTimeout);
  }

  public String getOwnerJID()
  {
    return this.jid;
  }

  public long getPacketReplyTimeout()
  {
    return this.packetReplyTimeout;
  }

  public void next(Form paramForm)
    throws XMPPException
  {
    executeAction(AdHocCommand.Action.next, paramForm, this.packetReplyTimeout);
  }

  public void prev()
    throws XMPPException
  {
    executeAction(AdHocCommand.Action.prev, this.packetReplyTimeout);
  }

  public void setPacketReplyTimeout(long paramLong)
  {
    this.packetReplyTimeout = paramLong;
  }
}